<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of VisitController
 *
 * @author gc
 */
class VisitController extends AdminController
{

    protected $student = '';

    public function _initialize()
    {
        parent::_initialize();
        $sid = I("sid");
        if (!empty($sid)) {
            session('sid', $sid);
        } else {
            $sid = session('sid');
        }
        $this->student = D("Student")->getStudentById($sid);
    }
    
    //来访记录
    public function showlist()
    {
        $map['status'] = array('eq', 1);

        $list = $this->lists('Visit', $map, 'create_time desc');
        $this->assign('_list', $list);

        $this->meta_title = ' 来访列表';

        $this->display();
    }

    public function index()
    {
        $map['sid'] = $this->student['id'];
        $map['status'] = array('eq', 1);

        $list = $this->lists('Visit', $map, 'create_time desc');
        $this->assign('_list', $list);

        $this->meta_title = $this->student['name'] . ' 来访记录';

        $this->display();
    }
    
    public function add()
    {
        $id = I("id");
        $meta_title = "{$this->student['name']} 新增来访记录";
        if (!empty($id)) {
            $map["id"] = $id;
            $data = M("Visit")->where($map)->find();
            $this->assign("data", $data);
            $meta_title = "{$this->student['name']} 编辑来访记录";
        }
        $this->meta_title = $meta_title;
        $this->display();
    }
    
    public function save()
    {
        $data = I("post.");
        $id = I("id");
        $data['sid'] = $this->student['id'];
        if (empty($id)) {
            $data['status'] = 1;
            $data['create_time'] = $data['update_time'] = time();
            $data['admin_id'] = UID;
            unset($data['id']);
        } else {
            $data['update_time'] = time();
        }

        $model = M("Visit");
        $s = empty($id) ? $model->add($data) : $model->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }
    
    public function delete()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Visit")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
